libname a "D:\SAS\Dataset\CRSP\Monthly Return";


data a (keep=date cusip ticker prc PRIMEXCH ret SHROUT VWRETD);
set a.Return_1952_1979 a.Return_1980_2020 a.Return_2021_2022;
run;




libname b  "D:\SAS\Dataset\Fama French Factors";

data b (keep=dateff rf);
set b.ff5factormonthly;

data b (rename=(dateff=date));
set b;
run;

proc sort data=a; by date; 
proc sort data=b; by date;


data c; merge a b; by date;
run;

data c;
set c;
year=year(date);
month=month (date);
me=prc *shrout;
stockreturn=ret-rf;
MKTRF=VWRETD-rf;
run;


data c1 (keep= year month me);
set c; where PRIMEXCH = "N";
run;

data c1;
set c1;
where month=6;
run;


proc sort data=c1; by year month;  


proc means data=c1 ; by year month;
var me; output out=c2 median=sizemedian;
run;

proc sort data=c2; by year month;

data c3 (drop=_TYPE_ _FREQ_);
set c2; by year month;
lsizemedian=lag (sizemedian);
run;

data c3 (drop=month);
set c3;


proc sort data=c; by year;
proc sort data=c3; by year;


data c;
merge c c3; by year;
run;


proc sort data=c; by year month; run;

data c;
set c;
if month<=6 and me<=lsizemedian then small=1;
if month<=6 and me>lsizemedian then small=0;
if month>6 and me<=sizemedian then small=1;
if month>6 and me>sizemedian then small=0;
run;


libname d "D:\SAS\Project\Persistence of MTB Ratio";

data d1 (keep=cusip fyear sgaintangible);
set d.sgaintangible;
run;

data d1 (rename=(fyear=year));
set d1;
run;



libname e  "D:\SAS\Dataset\Compustat\Compustat US";

data e1 (keep=DATADATE cusip at XRD);
set e.Compustat1950_1979 e.Compustat1980_1989 e.Compustat1990_1995 e.Compustat1996_2000 e.Compustat2001_2005 
e.Compustat2006_2010  e.Compustat2011_2020 e.compustat2021_2023;
run;


data e1;
set e1;
if XRD=. then xrd=0;
year=year(DATADATE);
run;

proc sort data=d1; by cusip year;
proc sort data=e1; by cusip year;


data d2;
merge d1 e1; by cusip year;
run;

proc sort data=d2; by cusip year;

data d2;
set d2; by cusip year;
lat=lag(at);
if first.cusip then lat=.;
if sgaintangible<0 then sgaintangible=0;
run;

data d2;
set d2;
intangible=(sgaintangible +xrd)/((lat+at)/2);
run;

proc sort data=d2; by cusip year;

data d2;
set d2; by cusip year;
lintangible=lag(intangible);
if first.cusip then lintangible =.;
run;


proc sort data=d2; by year lintangible;

proc rank data=d2 out=d3 group=3; by year;
var lintangible ;
ranks elintangible; 
run;

data d4 (keep=cusip year elintangible);
set d3;
where elintangible^=.;
cusip=substr(cusip, 1, 8);
run;


proc sort data=c; by cusip year;
proc sort data=d4; by cusip year;


data f;
merge c d4; by cusip year;
run;

data f;
set f;
if elintangible=. then delete;
if ret=. then delete;
if stockreturn=. then delete;
run;


proc sort data=f; by year month small elintangible; run; 

proc means data=f; by year month small elintangible;
output out=f1; 
var ret;
run;


data f1;
set f1;
where _STAT_ = "MEAN";
run;

data f2;
set f1;
where elintangible^=1;
run;


proc sort data=f2; by year month elintangible;

proc means data=f2; by year month elintangible;
output out=f3;
var ret;
run;

data f3;
set f3;
where _STAT_ = "MEAN";
run;


data f4 (keep=year month ret);
set f3;
where elintangible=2;
run;

data f4 (rename=(ret=rethigh));
set f4;
run;



data f5 (keep=year month ret);
set f3;
where elintangible=0;
run;

data f5 (rename=(ret=retlow));
set f5;
run;



proc sort data=f4; by year month;
proc sort data=f5; by year month;

data f6;
merge f4 f5;
by year month;
run;

data f6;
set f6;
SGAfactor=rethigh-retlow;
run;


proc sort data=c; by year month;
proc sort data=f6; by year month;


data g;
merge c f6; by year month;
run;


data g1 (keep=date SGAfactor);
set g;
where sgafactor^=.;
run;

proc sort data=g1 noduplicate; by date; run; 

data b.sgafactor;
set g1;
run;
